package com.contactbook.web.client;

import com.contactbook.web.client.dto.ContactDTO;
import com.contactbook.web.client.dto.MaritalStatusDTO;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class UpdateContactWidget extends VerticalPanel {
	
	private ContactBook parent;
	
	// Final DialogBox dialogBox;
	private static final String FIRST_NAME_TEXTBOX_SIZE = "125";
	private static final String LAST_NAME_TEXTBOX_SIZE = "125";
	private static final String EMAIL_TEXTBOX_SIZE = "150";
	private static final String ADDRESS_TEXTBOX_SIZE = "280";
	private static final String ZIPCODE_TEXTBOX_SIZE = "70";
	private static final String TOWN_TEXTBOX_SIZE = "110";
	private static final String COUNTRY_TEXTBOX_SIZE = "110";
	private static final String AGE_TEXTBOX_SIZE = "30";
	
	// Throwable error messages
	private static final String THROWABLE_ERROR_1 = "You must enter a firstname and a lastname for the contact.";
	private static final String THROWABLE_ERROR_2 = "Zipcode must be a number.";
	
	final TextBox firstnameTb, lastnameTb, emailTb, addressTb, zipCodeTb, townTb, countryTb, ageTb;
	final ListBox maritalStatusTb;
	
	public UpdateContactWidget(ContactBook parent) {
		
		super();
		this.parent = parent;

		/* Title label */
		
		final Label labelTitle = new Label("Update a contact");
		labelTitle.setHeight("30px");
		
		/* Form labels */
		
		final Label labelFirstname = new Label("First name :");
		final Label labelLastname = new Label("Last name :");
		final Label labelEmail = new Label("Email :");
		final Label labelAddress = new Label("Address :");
		final Label labelZipcode = new Label("Zip code :");
		final Label labelTown = new Label("Town :");
		final Label labelCountry = new Label("Country :");
		final Label labelAge = new Label("Age :");
		final Label labelMaritalstatus = new Label("Marital status :");
	
		/* Form items */
		
		firstnameTb = new TextBox();
		lastnameTb = new TextBox();
		emailTb = new TextBox();
		addressTb = new TextBox();
		zipCodeTb = new TextBox();
		townTb = new TextBox();
		countryTb = new TextBox();
		ageTb = new TextBox();	
		
		firstnameTb.setWidth(FIRST_NAME_TEXTBOX_SIZE);
		lastnameTb.setWidth(LAST_NAME_TEXTBOX_SIZE);
		emailTb.setWidth(EMAIL_TEXTBOX_SIZE);
		addressTb.setWidth(ADDRESS_TEXTBOX_SIZE);
		zipCodeTb.setWidth(ZIPCODE_TEXTBOX_SIZE);
		townTb.setWidth(TOWN_TEXTBOX_SIZE);
		countryTb.setWidth(COUNTRY_TEXTBOX_SIZE);		
		ageTb.setWidth(AGE_TEXTBOX_SIZE);	

		
		maritalStatusTb = new ListBox();
		maritalStatusTb.addItem("");
		maritalStatusTb.addItem("Single");
		maritalStatusTb.addItem("Married");
		maritalStatusTb.addItem("Separated");
		maritalStatusTb.addItem("Divorced");
		maritalStatusTb.addItem("Widowed");
		maritalStatusTb.addItem("Engaged");
		maritalStatusTb.addItem("Annulled");
		maritalStatusTb.addItem("Cohabitating");
		
		// TODO : final ListBox groupTb = new ListBox();
		
		/* Functional constraints */
		zipCodeTb.setMaxLength(5);
		ageTb.setMaxLength(3);
		
		HorizontalPanel panelFirstName = new HorizontalPanel();
		panelFirstName.add(labelFirstname);
		panelFirstName.add(firstnameTb);
		
		HorizontalPanel panelLastName = new HorizontalPanel();
		panelLastName.add(labelLastname);
		panelLastName.add(lastnameTb);
		
		HorizontalPanel panelEmail = new HorizontalPanel();
		panelEmail.add(labelEmail);
		panelEmail.add(emailTb);
		
		HorizontalPanel panelAddress = new HorizontalPanel();
		panelAddress.add(labelAddress);
		panelAddress.add(addressTb);

		HorizontalPanel panelZipCode = new HorizontalPanel();
		panelZipCode.add(labelZipcode);
		panelZipCode.add(zipCodeTb);
		
		HorizontalPanel panelTown = new HorizontalPanel();
		panelTown.add(labelTown);
		panelTown.add(townTb);
		
		HorizontalPanel panelCountry = new HorizontalPanel();
		panelCountry.add(labelCountry);
		panelCountry.add(countryTb);
		
		HorizontalPanel panelAge = new HorizontalPanel();
		panelAge.add(labelAge);
		panelAge.add(ageTb);
		
		HorizontalPanel panelMaritalStatus = new HorizontalPanel();
		panelMaritalStatus.add(labelMaritalstatus);
		panelMaritalStatus.add(maritalStatusTb);
		
		final Button buttonOk = new Button("Ok");
//		buttonOk.addClickHandler(new OkHandler());
		final Button buttonCancel = new Button("Annuler");
	//	buttonCancel.addClickHandler(new CancelHandler());

		
		HorizontalPanel panelButtons = new HorizontalPanel();
		panelButtons.add(buttonOk);
		panelButtons.add(buttonCancel);
			
		add(labelTitle);
		add(panelFirstName);
		add(panelLastName);
		add(panelEmail);
		add(panelAddress);
		add(panelZipCode);
		add(panelTown);
		add(panelCountry);
		add(panelAge);
		add(panelMaritalStatus);
		add(panelButtons);
		
		
		RootPanel.get("app-content").add(this);
		
	}
}
		
		
		/* Form */
		/*
		RootPanel.get("firstname").add(firstnameTb);
		firstnameTb.setFocus(true);
		
		RootPanel.get("lastname").add(lastnameTb);		
		RootPanel.get("email").add(emailTb);		
		RootPanel.get("address").add(addressTb);		
		RootPanel.get("zipcode").add(zipCodeTb);		
		RootPanel.get("town").add(townTb);		
		RootPanel.get("country").add(countryTb);
		RootPanel.get("age").add(ageTb);
		RootPanel.get("maritalstatus").add(maritalStatusTb);
		
	}
	
	private class CancelHandler implements ClickHandler {
		public void onClick(ClickEvent event) {
			firstnameTb.setText("");
			lastnameTb.setText("");
			emailTb.setText("");
			addressTb.setText("");
			zipCodeTb.setText("");
			countryTb.setText("");
			ageTb.setText("");
			//maritalStatusTb.setSelectedIndex(0);
		}
	}
	
	private class OkHandler implements ClickHandler {
		public void onClick(ClickEvent event) {
			String firstname = firstnameTb.getText();
			String lastname = lastnameTb.getText();
			String email = emailTb.getText();
			String address = addressTb.getText();
			String zipcode = zipCodeTb.getText();
			String town = townTb.getText();
			String country = countryTb.getText();
			String age = ageTb.getText();
		//	int maritalstatus = maritalStatusTb.getSelectedIndex();
			
			if((!isNaN(zipcode) && !zipcode.isEmpty()) || zipcode.isEmpty()) {
				if(!firstname.isEmpty() && !lastname.isEmpty()) {
					ContactDTO contactDTO = new ContactDTO();
					contactDTO.setFirstName(firstname);
					contactDTO.setLastName(lastname);
					contactDTO.setEmail(email);
					contactDTO.setAddress(address);
					contactDTO.setZipCode(Integer.parseInt(zipcode));
					contactDTO.setTown(town);
					contactDTO.setCountry(country);
					contactDTO.setAge(Integer.parseInt(age));
/*
					switch(maritalstatus) {
						case 0: contactDTO.setMaritalStatus(null);
							break;
						case 1: contactDTO.setMaritalStatus(MaritalStatusDTO.SINGLE);
							break;
						case 2: contactDTO.setMaritalStatus(MaritalStatusDTO.MARRIED);
							break;
						case 3: contactDTO.setMaritalStatus(MaritalStatusDTO.SEPARATED);
							break;
						case 4: contactDTO.setMaritalStatus(MaritalStatusDTO.DIVORCED);
							break;
						case 5: contactDTO.setMaritalStatus(MaritalStatusDTO.WIDOWED);
							break;
						case 6: contactDTO.setMaritalStatus(MaritalStatusDTO.ENGAGED);
							break;
						case 7: contactDTO.setMaritalStatus(MaritalStatusDTO.ANNULLED);
							break;
						default: break;						
					}
					
					contactDTO.setGroup(null);
					parent.addContact(contactDTO);
				} else {
					parent.getAddContactCallBack().onFailure(new Throwable(THROWABLE_ERROR_1));
				}
			} else {
				parent.getAddContactCallBack().onFailure(new Throwable(THROWABLE_ERROR_2));
			}
		}
	}

	private boolean isNaN(String value) {
		for (char c : value.toCharArray())
			if (c < '0' || c > '9')
				return true;
		return false;	
	}
}
*/